package com.lxq.market.servlet;

import com.lxq.market.model.User;
import com.lxq.market.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    UserService service=UserService.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("user_name");
        String userPassword = req.getParameter("user_password");
        User user=new User();
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        boolean registerResult = service.addUser(user);
        if (registerResult){
            req.setAttribute("new_user",user);
            req.getRequestDispatcher("/WEB-INF/result.jsp").forward(req,resp);
        }
        else{
            req.setAttribute("unsuccessfully_register_result","未知原因，注册失败");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
        }
    }
}
